<%@ page language="java" import="java.util.*,ll.sms.util.*"
	pageEncoding="UTF-8"%>
<%
	String currentUserChannel = SmsUtil.sessionHelper(session,
			AdminProperty.adefaultchannel);
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>通道变更</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<link rel="stylesheet" type="text/css" href="../ext/ext-all.css" />
		<script type="text/javascript" src="../ext/ext-base.js"></script>
		<script type="text/javascript" src="../ext/ext-all.js"></script>
		<style type="text/css">
.settings {
	background: url(../images/slate/grid/refresh.gif) left top
		no-repeat !important;
}
</style>
		<script type="text/javascript">
	  Ext.BLANK_IMAGE_URL = '../images/s.gif';
	  Ext.onReady(function(){
	  var dchannel = <%=currentUserChannel%>;
	  var reader=new Ext.data.JsonReader({
				root:'result',
				totalProperty:'totalCount',
				fields:['proxyid','proxyname','channelid','channelservicename','channelshowname']
			});

	function getChannelCombox(){
	var cstore = new Ext.data.SimpleStore({
	fields: ['clazz', 'showname'],
	data : <%=ExtJsonBuilder.getAllChannels()%>
	});

    var comb = new Ext.form.ComboBox({
            emptyText : '需要被变更的通道',
            store: cstore,
            mode:'local',
            triggerAction:'all',
            valueField: 'clazz',
            name:'_cchannel',
            readOnly : true,
            id:'_cchannel',
            hiddenName : 'cchannel',
            displayField:'showname',
 			fieldLabel: '需要被变更的通道',
 			withd :140

            });
       return comb;
	}

	function getChannelCombox_1(){

		var cstore = new Ext.data.SimpleStore({
		fields: ['clazz', 'showname'],
		data : <%=ExtJsonBuilder.getAllChannels()%>
		});

	    var comb = new Ext.form.ComboBox({
	            emptyText : '通道变更为',
	            store: cstore,
	            mode:'local',
	            triggerAction:'all',
	            valueField: 'clazz',
	            name:'_cchannel_1',
	            readOnly : true,
	            id:'_cchannel_1',
	            hiddenName : 'cchannel_1',
	            displayField:'showname',
	 			fieldLabel: '通道变更为',

	 			withd :180

	            });
	       return comb;
	}

	 var store=new Ext.data.GroupingStore({
			id:'GroupStore',
			reader: reader,
			remoteSort:true,
            sortInfo:{field: 'proxyid',direction: 'ASC'},
            groupField:['proxyid'],
			proxy:new Ext.data.HttpProxy({
				url:'../ajaxjsp/channelchange.jsp?cmd=getlist',
				autoAbort:true,
				disableCaching:true,
				timeout:180000,
				method:'POST'
			})
        });
		store.load();

  var grid = new Ext.grid.GridPanel({
        store: store,
        columns: [
				new Ext.grid.RowNumberer(),//获得行号
				{header: "代理ID", sortable:true,width: 300, dataIndex:"proxyid",hidden:true},
				{header: "通道ID", sortable:true,width: 300, dataIndex:"channelid",hidden:true},
				{header: "通道自定义名", sortable:true,width: 300, dataIndex:"channelshowname"},
				{header: "通道服务器名", sortable:true,width: 300, dataIndex:"channelservicename"}

        ],

        view: new Ext.grid.GroupingView({
            forceFit:true,
            groupTextTpl: '{[ values.rs[0].data["proxyname"]]} ({[values.rs.length]} 条记录)'
        }),
         tbar: new Ext.Toolbar({
 			items:[
 			{
 				id:'proxyname',
                name: 'proxyname',
                xtype:'textfield'
            },
 			{
 			id : 'search',
 			text : '查询代理',
 			handler: function(){
 			store.reload({params:{proxyname:Ext.get('proxyname').dom.value}});
 			}
 			},'->',{
 			id : 'groblechannelchange',
 			iconCls:'settings',
 			text : '全局通道替换',
 			handler: function(){

							var cb =  getChannelCombox();
							var tocb = getChannelCombox_1();

    		                var nav = new Ext.Panel({
							            //title       : 'Navigation',
							            //split       : true,
							            //width       : 200,
							            //layout      : 'column',
							            layout: 'form',
							            collapsible : true,
							          	items :[
							          	 		{
								                id:'_t',
								                html:'<hr><font color=\"red\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;你即将将某通道进行全局替换通道进行更换，<br>该替换将使所有使用了该通道的代理以及该代理下的所有使用该通道用户和消息全部替换！</font><hr>'
								                },
								                cb,
								                tocb,
								                {
								                id:'_b',
								                html:'<br>'
								                },
								                  new Ext.Button({

											        text: '确定',
											        handler: function()
											        {

														   if(Ext.isEmpty(cb.getValue()) || Ext.isEmpty(tocb.getValue())){
																Ext.Msg.alert('警告','你未完全选择通道');
																return;
															}
															if(cb.getValue()==tocb.getValue()){
															Ext.Msg.alert('警告','你选择了相同的通道');
															return;
															}
														   Ext.MessageBox.confirm("警告", "您确定要将所有使用到通道【"+Ext.get('_cchannel').dom.value+"】的地方全部替换为【"+Ext.get('_cchannel_1').dom.value+"】吗？", function(btn){
													                if (btn == 'yes') {
													                 Ext.Ajax.request({
													                  url : '../ajaxjsp/channelchange.jsp',
													                  method : 'POST',
													                  params : {
													                      cmd : 'doallproxychange',
													                      channel : cb.getValue(),
													                      tochannel : tocb.getValue()
													                  },
													                  success: function(response, options) {
													                        //获取响应的json字符串
													                       var responseArray = Ext.util.JSON.decode(response.responseText);
													                            if(responseArray.success==true){
													                            	store.reload();
																					Ext.Msg.alert('成功','操作成功');
													                            }
													                            else{
													                                Ext.Msg.alert('失败','操作失败');
													                            }
													                         }
													              });
													             }
													            });
											        }
											    })
							          	]
							        });

									var winu = new Ext.Window({
								           		title: '全局通道变换',
								                //applyTo     : 'auto-user',
								                layout      : 'fit',
								                width       : 400,
								                height      : 250,
								                closeAction :'close',
								                plain       : true,

												items : [
													nav
												],
								                 buttons: [
													{
								                    text     : '关闭',
								                    handler  : function(){ winu.close();}
								                    }
												]
								            });
								            winu.show();
 			}
 			}]
        }),
        frame:true,
        width: 900,
        height: 600,
        collapsible: true,
        animCollapse: false,
        title: '代理通道列表',
        renderTo: document.body
    });

      grid.on("cellclick",function(grid, rowIndex, columnIndex, e)
    	     {
	    	 e.preventDefault();if(rowIndex<0){return;}
	         var record = grid.getStore().getAt(rowIndex);   //Get the Record
			 var fieldName = grid.getColumnModel().getDataIndex(columnIndex); //Get field name
			 var data = record.get(fieldName);

	         var treeMenu = new Ext.menu.Menu
    	            ([
    	                {xtype:"button",text:"变更该通道",icon:"../images/default/tree/drop-between.gif",pressed:true,handler:function(){
							var cb =  getChannelCombox();
    		                var nav = new Ext.Panel({
							            //title       : 'Navigation',
							            //split       : true,
							            //width       : 200,
							            //layout      : 'column',
							            collapsible : true,
							          	items :[
							          	 		{
								                id:'_t',
								                html:'<hr><font color=\"red\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;你即将将代理【'+record.get('proxyname')+"】的【"+record.get('channelshowname')+"】通道进行更换，<br>该替换将使该代理以及该代理下的所有使用该通道处全部替换！</font><hr>"
								                },{
								                id:'_a',
								                html:'请选择你需要替换变更的通道'

								                },cb
								               ,{
								                id:'_b',
								                html:'<br>'

								                },
								                  new Ext.Button({

											        text: '确定',
											        handler: function()
											        {

														   var tcount = store.getCount();
														   var fb = false;
														   for(var k=0;k<tcount;k++){
														   	   var rc = store.getAt(k);
														   	   if(rc.get('proxyid')==record.get('proxyid')){
																	if(rc.get('channelid')==cb.getValue()){
																		fb=true;
																	}
														   	   }

														   }

														   if(Ext.isEmpty(cb.getValue())){
																Ext.MessageBox.alert("警告","你未选择通道");
																return;
														   }
														   if(record.get('channelid')==cb.getValue()){
														  		 Ext.MessageBox.alert("警告","你选择了相同的通道");
																return;
														   }
														   if(fb){
														   		alert('警告，你选择的通道该代理已经配置了！');
														   }

														   Ext.MessageBox.confirm("警告", "您确定要将通道【"+record.get('channelshowname')+"】替换为【"+Ext.get('_cchannel').dom.value+"】吗？", function(btn){
													                if (btn == 'yes') {
													                 Ext.Ajax.request({
													                  url : '../ajaxjsp/channelchange.jsp',
													                  method : 'POST',
													                  params : {
													                      cmd : 'doproxychange',
													                      proxyid :record.get('proxyid'),
													                      channel : record.get('channelid'),
													                      tochannel : cb.getValue()
													                  },
													                  success: function(response, options) {
													                        //获取响应的json字符串
													                       var responseArray = Ext.util.JSON.decode(response.responseText);
													                            if(responseArray.success==true){
													                            	store.reload();
																					Ext.Msg.alert('成功','操作成功');
													                            }
													                            else{
													                                Ext.Msg.alert('失败','操作失败');
													                            }
													                         }
													              });
													             }
													            });
											        }
											    })
							          	]
							        });
								var winu = new Ext.Window({
								           		title: '通道更换',
								                //applyTo     : 'auto-user',
								                layout      : 'fit',
								                width       : 400,
								                height      : 250,
								                closeAction :'close',
								                plain       : true,

								               /* items       : [
								                {
								                id:'_t',
								                html:'<hr><font color=\"red\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;你即将将代理【'+record.get('proxyname')+"】的【"+record.get('channelshowname')+"】通道进行更换，<br>该替换将使该代理以及该代理下的所以使用该通道处全部替换！</font><hr>"
								                },getChannelCombox()
								                ],*/
												items : [
													nav
												],
								                 buttons: [
													{
								                    text     : '关闭',
								                    handler  : function(){ winu.close();}
								                    }
												]
								            });
								            winu.show();

    	                }}
    	            ]);
    	            treeMenu.showAt(e.getPoint());
    	        });
});

  </script>
	</head>

	<body>

	</body>
</html>
